package controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import model.User;
import repository.UserRepository;
import service.HobbyService;
import service.UserService;

import java.util.List;

@RestController
@RequestMapping("api/user")
@ComponentScan
public class UserController {
    protected final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private HobbyService hobbyService;

    @RequestMapping(value="", method=RequestMethod.GET)
    @ResponseBody
    public List<User> getAllUsers() {
        log.info("Get all users");
        return userRepository.findAll();
    }

    @RequestMapping(value="/{id}", method=RequestMethod.GET)
    @ResponseBody
    public User getUserById(@PathVariable("id") long id) {
        log.info("Get user by Id");
        return userRepository.findOne(id);
    }

    @RequestMapping(value="/{id}", method=RequestMethod.PUT)
    @ResponseBody
    public User updateUser(@RequestBody final User user) {
        log.info("Updating a user");
        return userRepository.save(user);
    }

    @RequestMapping(value="", method=RequestMethod.POST)
    @ResponseBody
    public User saveUser(@RequestBody final User user) {
        log.info("Saving a user");

        return userRepository.save(user);
    }

    @RequestMapping(value="/{id}", method=RequestMethod.DELETE)
    public void deleteUser(@PathVariable("id") long id) {
        log.info("Deleting a user");
        userRepository.delete(id);
    }
}